package com.you.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.you.common.QueryPageParam;
import com.you.common.Result;
import com.you.entity.Goods;
import com.you.service.impl.GoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 友友
 * @since 2022-10-24
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    GoodsServiceImpl goodsService;

    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Goods goods) {
        return goodsService.save(goods)? Result.suc():Result.fail();
    }

    //修改
    @PostMapping("/mod")
    public Result update(@RequestBody Goods goods) {
        return goodsService.updateById(goods)? Result.suc():Result.fail();
    }

    //删除
    @GetMapping("/del")
    public Result del(@RequestParam Integer id) {
        return goodsService.removeById(id)? Result.suc():Result.fail();
    }

    //自定义分页(查询)
    @PostMapping("/diyListPage")
    public Result diyListPage(@RequestBody QueryPageParam pageParam) {

        HashMap param = pageParam.getParam();
        String name = (String) param.get("name");
        String storage = (String) param.get("storage");
        String goodstype = (String) param.get("goodstype");

        Page<Goods> page = new Page<>();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());

        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(name)){
            wrapper.like(Goods::getName, name);
        }
        if (StringUtils.isNotBlank(storage)){
            wrapper.eq(Goods::getStorage, storage);
        }
        if (StringUtils.isNotBlank(goodstype)){
            wrapper.eq(Goods::getGoodstype, goodstype);
        }

        IPage result = goodsService.pageDiy(page, wrapper);

        return Result.suc(result.getRecords(),result.getTotal());

    }
}


